//
//  SWWelcomeController.swift
//  MYSwiftWeiBo
//
//  Created by shingwai chan on 16/2/29.
//  Copyright © 2016年 ChanShingWai. All rights reserved.
//

import UIKit
import SDWebImage

class SWWelcomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        setUpUI()
        
        setIconImage()
        
        SWUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                print("获取头像失败： \(error)")
                return
            }
            
            self.setIconImage()
        }
        
        
    }
    
    func setIconImage() {
    
        if let avatar_large = SWUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            print("\(avatar_large)")
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        viewAnimation()
        
    }
    
    var iconViewBottomConstraint :NSLayoutConstraint?
    //设置子控件
    func setUpUI() {
        
        view.addSubview(backGroudView)
        view.addSubview(iconView)
        view.addSubview(messagerLabel)
        
        //添加约束
        backGroudView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messagerLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //背景
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backGroundView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["backGroundView" : backGroudView]))
       
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backGroundView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["backGroundView" : backGroudView]))
        
        //头像
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //头像离底部的约束
        iconViewBottomConstraint =  NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -300)
        view.addConstraint(iconViewBottomConstraint!)
        //头像大小
        iconView.snp_makeConstraints { (make) -> Void in
            make.size.width.height.equalTo(80)
        }
        
        
        
        //label
        view.addConstraint(NSLayoutConstraint(item: messagerLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        
        view.addConstraint(NSLayoutConstraint(item: messagerLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
    
    }
    
    //设置动画
    func viewAnimation() {
        
        iconViewBottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height)+200
        
        UIView.animateWithDuration(1.2, delay: 0.1, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.iconView.alpha = 1
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //lebel
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messagerLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //动画完成后跳到MainVC
                        AppDelegate.outSideChoeseRootViewController(true)
                })
        }
        
    }
    
    
    //MARK: - 控件懒加载
    lazy var iconView: UIImageView = {
       let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        icon.layer.cornerRadius = 42.5
        icon.layer.masksToBounds = true
        icon.alpha = 0
        return icon
    }()
    
    lazy var backGroudView = UIImageView(image: UIImage(named: "ad_background"))
    
    lazy var messagerLabel: UILabel = {
        
        let label = UILabel()
        label.text = "欢迎回来"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.alpha = 0
        label.sizeToFit()
        return label
    }()
    
    

}
